package org.example;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DoubleColorBallApiCrawler {
    public static void main(String[] args) {
        String apiUrl = "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=3000&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=3&pageSize=1000&systemType=PC"; // 替换为真实API
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(apiUrl);
            CloseableHttpResponse response = client.execute(request);

            // 解析JSON
            String json = EntityUtils.toString(response.getEntity());
            ObjectMapper mapper = new ObjectMapper();
            JsonNode root = mapper.readTree(json);

            // 遍历数据
            for (JsonNode item : root.get("result")) {
                String issue = item.get("code").asText();
                String redBalls = item.get("red").asText();
                String blueBall = item.get("blue").asText();
                String date = item.get("date").asText();

                System.out.print("日期: " + date+" ");
                System.out.print("期数: " + issue+" ");
                System.out.print("红球: " + redBalls+" ");
                System.out.print("蓝球: " + blueBall);
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}